package com.iims.customer;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.constant.UserConstants;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.customer.domain.Customer;
import com.iims.customer.service.ICustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:33
 */
@Api(tags = "E-客户管理")
@RestController
@RequestMapping("/business/customer")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;

    /**
     * 获取客户列表
     */
    @ApiOperation("分页查询客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "客户名称[模糊匹配]", name = "customerName", paramType = "query"),
            @ApiImplicitParam(value = "客户类型Id[精确匹配]", name = "cusTypeId", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore Customer customer) throws Exception {
        startPage();
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 根据客户编号获取详细信息
     */
    @ApiOperation("根据客户编号获取详细信息")
    @ApiImplicitParam(name = "customerId", value = "客户Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:customer:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable Long customerId) {
        return AjaxResult.success(customerService.selectOne(customerId));
    }

    /**
     * 新增客户
     */
    @ApiOperation("新增客户")
    @ApiOperationSupport(ignoreParameters = {"customerId", "cusTypeName"})
    @PreAuthorize("@ss.hasPermi('business:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Customer customer) {
        if (UserConstants.NOT_UNIQUE.equals(customerService.checkCustomerNameUnique(customer))) {
            return AjaxResult.error("新增客户'" + customer.getCustomerName() + "'失败，客户名称已存在");
        }
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @ApiOperation("修改客户")
    @ApiOperationSupport(ignoreParameters = {"cusTypeName"})
    @PreAuthorize("@ss.hasPermi('business:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Customer customer) {
        if (UserConstants.NOT_UNIQUE.equals(customerService.checkCustomerNameUnique(customer))) {
            return AjaxResult.error("修改客户'" + customer.getCustomerName() + "'失败，客户名称已存在");
        }
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @ApiOperation("删除客户")
    @PreAuthorize("@ss.hasPermi('business:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerId}")
    public AjaxResult remove(@ApiParam("客户Id") @PathVariable Long customerId) {
        if (customerService.checkCustomerUse(customerId)) {
            return AjaxResult.error("删除客户失败，客户已使用！请先删除关联客户的合同！");
        }
        return toAjax(customerService.deleteCustomerById(customerId));
    }
}
